<?php

/**
 * @package Spider Contacts
 * @author Web-Dorado
 * @copyright (C) 2012 Web-Dorado. All rights reserved.
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 **/

defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.model' );



class spidercontactsModelshowtable extends JModel

{



function showPublishedContacts()

{



$option=JRequest::getVar('option');



$param_categories=JRequest::getVar('categories', 0);



$params = new jsshparams;



$doc =& JFactory::getDocument();



$cont_in_page=$params->get( 'count_of_rows_in_the_table' );
$search = JRequest::getVar('name_search','');


$page_num=JRequest::getVar('page_num', 1);$cat_id=JRequest::getVar('cat_id', 0);

$db =& JFactory::getDBO();



if($param_categories>0){



$query_count = "SELECT count(#__spidercontacts_contacts.id) as cont_count FROM #__spidercontacts_contacts left join #__spidercontacts_contacts_categories on #__spidercontacts_contacts.category_id=#__spidercontacts_contacts_categories.id WHERE

#__spidercontacts_contacts.published = '1'  and #__spidercontacts_contacts.category_id='".$db->getEscaped($param_categories)."' ";



$query = "SELECT #__spidercontacts_contacts.*, #__spidercontacts_contacts_categories.name as cat_name FROM #__spidercontacts_contacts left join #__spidercontacts_contacts_categories on #__spidercontacts_contacts.category_id=#__spidercontacts_contacts_categories.id WHERE

#__spidercontacts_contacts.published = '1'  and #__spidercontacts_contacts.category_id='".$db->getEscaped($param_categories)."' ";

}
else

{

$query_count = "SELECT count(#__spidercontacts_contacts.id) as cont_count FROM #__spidercontacts_contacts  WHERE

#__spidercontacts_contacts.published = '1' ";



$query= "SELECT #__spidercontacts_contacts.*, #__spidercontacts_contacts_categories.name as cat_name  FROM #__spidercontacts_contacts left join #__spidercontacts_contacts_categories on #__spidercontacts_contacts.category_id=#__spidercontacts_contacts_categories.id WHERE

#__spidercontacts_contacts.published = '1' ";if($cat_id!=0){$query_count .= " and #__spidercontacts_contacts.category_id='".$db->getEscaped($cat_id)."' ";$query .= " and #__spidercontacts_contacts.category_id='".$db->getEscaped($cat_id)."' ";}

}

if ($search)
{
	$query.= " AND concat(first_name,' ', last_name) LIKE '%".$db->getEscaped($search)."%'";
	$query_count.= " AND concat(first_name,' ', last_name) LIKE '%".$db->getEscaped($search)."%'";
	}

$query .= " limit ".(($page_num-1)*$cont_in_page).",$cont_in_page ";

$db->setQuery($query_count);



$row = $db->loadAssoc();



$cont_count=$row['cont_count'];





$db->setQuery($query);



$rows = $db->loadObjectList();




if ($db->getErrorNum())

{

echo $db->stderr();

return false;

}

foreach($rows as $row)

{



	$id=$row->id;



	

		$query= "SELECT * FROM #__spidercontacts_contacts_categories WHERE id = '".$db->getEscaped($row->category_id)."' ";	$db->setQuery($query);	$row2 = $db->loadAssoc();	



	$query= "SELECT * FROM #__spidercontacts_contacts_categories WHERE id = '".$db->getEscaped($row->category_id)."' ";



	$db->setQuery($query);



	$row2 = $db->loadAssoc();

	$categories[$row2['id']]=$row2['name'];			

	
}


	$query= "SELECT * FROM #__spidercontacts_contacts_categories WHERE `published`=1 ";	$db->setQuery($query);	$category_list = $db->loadObjectList();



return array($rows, $option,$params,$page_num,$cont_count,@$categories,$cont_in_page,$category_list,$param_categories);




}
}

?>

